const { TreeNode, BinaryTreeTraversal } = require("../template")

function testBinaryTreeComplexTrees() {
  const traversal = new BinaryTreeTraversal()

  console.log(
    "1. 大深度树测试:",
    (() => {
      let deepTree = new TreeNode(1)
      let current = deepTree
      for (let i = 2; i <= 10; i++) {
        current.right = new TreeNode(i)
        current = current.right
      }
      return traversal.maxDepth(deepTree) === 10 &&
             traversal.isBalanced(deepTree) === false
    })()
  )

  console.log(
    "2. 完全二叉树测试:",
    (() => {
      // 构建完全二叉树:    1
      //                  /   \
      //                 2     3
      //                / \   / \
      //               4   5 6   7
      const completeTree = new TreeNode(1)
      completeTree.left = new TreeNode(2)
      completeTree.right = new TreeNode(3)
      completeTree.left.left = new TreeNode(4)
      completeTree.left.right = new TreeNode(5)
      completeTree.right.left = new TreeNode(6)
      completeTree.right.right = new TreeNode(7)
      
      return JSON.stringify(traversal.preorder(completeTree)) === JSON.stringify([1, 2, 4, 5, 3, 6, 7]) &&
             JSON.stringify(traversal.inorder(completeTree)) === JSON.stringify([4, 2, 5, 1, 6, 3, 7]) &&
             JSON.stringify(traversal.postorder(completeTree)) === JSON.stringify([4, 5, 2, 6, 7, 3, 1]) &&
             JSON.stringify(traversal.levelOrder(completeTree)) === JSON.stringify([[1], [2, 3], [4, 5, 6, 7]]) &&
             traversal.maxDepth(completeTree) === 3 &&
             traversal.isBalanced(completeTree) === true
    })()
  )

  console.log(
    "3. 复杂不平衡树测试:",
    (() => {
      // 构建复杂树:    1
      //              /   \
      //             2     3
      //            / \   /
      //           4   5 6
      //          /     \
      //         7       8
      //        /
      //       9
      const complexTree = new TreeNode(1)
      complexTree.left = new TreeNode(2)
      complexTree.right = new TreeNode(3)
      complexTree.left.left = new TreeNode(4)
      complexTree.left.right = new TreeNode(5)
      complexTree.right.left = new TreeNode(6)
      complexTree.left.left.left = new TreeNode(7)
      complexTree.left.right.right = new TreeNode(8)
      complexTree.left.left.left.left = new TreeNode(9)
      
      return traversal.maxDepth(complexTree) === 5 &&
             traversal.isBalanced(complexTree) === false
    })()
  )

  console.log(
    "4. 性能测试:",
    (() => {
      const performanceTree = new TreeNode(1)
      const nodes = [performanceTree]
      for (let i = 2; i <= 100; i++) {
        const parent = nodes[Math.floor((i - 2) / 2)]
        const newNode = new TreeNode(i)
        if (i % 2 === 0) {
          parent.left = newNode
        } else {
          parent.right = newNode
        }
        nodes.push(newNode)
      }
      
      const start = performance.now()
      const depth = traversal.maxDepth(performanceTree)
      const isBalanced = traversal.isBalanced(performanceTree)
      const end = performance.now()
      
      return depth > 0 && depth < 100 && (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "5. 大树的遍历一致性测试:",
    (() => {
      const largeTree = new TreeNode(1)
      largeTree.left = new TreeNode(2)
      largeTree.right = new TreeNode(3)
      largeTree.left.left = new TreeNode(4)
      largeTree.left.right = new TreeNode(5)
      largeTree.right.left = new TreeNode(6)
      largeTree.right.right = new TreeNode(7)
      
      const preorder = traversal.preorder(largeTree)
      const inorder = traversal.inorder(largeTree)
      const postorder = traversal.postorder(largeTree)
      const levelOrder = traversal.levelOrder(largeTree)
      
      return preorder.length === 7 &&
             inorder.length === 7 &&
             postorder.length === 7 &&
             levelOrder.length === 3 &&
             levelOrder[0].length === 1 &&
             levelOrder[1].length === 2 &&
             levelOrder[2].length === 4
    })()
  )

  console.log(
    "6. this上下文复杂树测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        traversal: new BinaryTreeTraversal(),
        processComplexTree: function(root) {
          const depth = this.traversal.maxDepth(root)
          return depth * this.multiplier
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      testTree.left.left = new TreeNode(4)
      return complexObj.processComplexTree(testTree) === 9 // 3 * 3 = 9
    })()
  )

  console.log(
    "7. 复杂对象this绑定复杂树测试:",
    (() => {
      const complexConfigObj = {
        config: { threshold: 3 },
        traversal: new BinaryTreeTraversal(),
        processWithConfig: function(root) {
          const depth = this.traversal.maxDepth(root)
          return depth > this.config.threshold
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      testTree.left.left = new TreeNode(4)
      testTree.left.right = new TreeNode(5)
      return complexConfigObj.processWithConfig(testTree) === true // 深度3 > 阈值3
    })()
  )
}

testBinaryTreeComplexTrees()
